<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MY_Controller {
	private $table_ = '' ;
	private $ROOT_ = '' ;
	private $username ;
	function Users(){
		
		parent::__construct();
		$this->ROOT_ = str_replace("\\", "/", FCPATH);
		$this->load->model('M_upload');
		date_default_timezone_set('Asia/Shanghai');
		$this->load->library('common');//加载admin的公用方法
		$this->table_ = $this->common->table_pre('real_data');
		$this->username = login_name() ;
	
		
	}
	public function index()
	{
		

		$action = $this->input->post("action",true) ; 
		if($action == 'ajax'){
			$this->ajax_data();
			die();
		}
		$data['username'] = $this->username ;
		$this->load->view('views_index');//加载首页
	}
	private function  ajax_data(){
		
		$sql_ = "SELECT * FROM `{$this->table_}extra_filelist`" ;
		$post_data = $this->input->post(NULL,true) ;
		$where = ' where 1=1 and `status` = 1 ';
		if(!isset($post_data['page'])){
			$page  =1 ;
		}else{
			$page = intval($post_data['page']);
		}
	
		$sql_count = "SELECT count(*) as tt FROM {$this->table_}extra_filelist $where";
		
		$total = $this->M_upload->query_count($sql_count);
		
		$per_page = 10;//每一页显示的数量
		$page_string = $this->page_string($total, $per_page, $page);
		$limit = ($page-1)*$per_page;
		$sql = "SELECT * FROM {$this->table_}extra_filelist $where  ORDER BY id DESC LIMIT ".$limit.",".$per_page;
		$res = $this->M_upload->querylist($sql);
	
		$data['result'] = $res ;
		$data['page_string'] = $page_string ;	
		
		echo result_to_towf_new($data,'0','',null);
	}
	function edit_num(){
		$id = $this->input->get_post("id",true) ;
		if($id<=0){return ;}
		$sql_update = "UPDATE {$this->table_}extra_filelist SET num = num +1 where id = '{$id}'";
		$this->M_upload->update_data($sql_update);
	}


	/*@function page_string
	 *@分页返回数据
	 *@total 总的数量
	 *@page_num 每一页显示的数量
	 *@page 当前是第几页数据
	 */
	private function page_string($total,$page_num,$page){
		$page_string = '' ;
		$this->load->library('pagination');//加载分页类
		$this->load->library('MY_Pagination');//加载分页类
		$config['total_rows'] = $total;
		$config['use_page_numbers'] =true; // 当前页结束样式
		$config['per_page'] = $page_num; // 每页显示数量，为了能有更好的显示效果，我将该数值设置得较小
		$config['full_tag_open'] = '<div class="pg">'; // 分页开始样式
		$config['full_tag_close'] = '</div>'; // 分页结束样式
		$config['first_link'] = '首页'; // 第一页显示
		$config['last_link'] = '末页'; // 最后一页显示
		$config['next_link'] = '下一页 >'; // 下一页显示
		$config['prev_link'] = '< 上一页'; // 上一页显示
		$config['cur_tag_open'] = ' <a class="current">'; // 当前页开始样式
		$config['cur_tag_close'] = '</a>'; // 当前页结束样式
		$config['uri_segment'] = 6;
		$config['anchor_class']='class="ajax_page" ';
		$this->pagination->cur_page = $page ;
		$this->pagination->initialize($config); // 配置分页
		$page_string =  $this->pagination->create_links(true,'ajax_data');
		return $page_string ;
	}




}

